package com.annotation.conditional;


import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition implements Condition {

    /**
     * ConditionContext:判断条件能使用的上下文环境
     * AnnotatedTypeMetadata:注解信息
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取IOC容器使用的beanfactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //获取容器中是否有person的声明（容器中所有的bean先声明，然后再实例，所以使用该方法不用担心bean还没有被IOC声明而获取不到）
        System.out.println("是否包含person的定义" + beanFactory.containsBean("person"));
        //获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        //获取当前环境信息
        Environment environment = context.getEnvironment();
        //获取bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        String osName = environment.getProperty("os.name");
        System.out.println(osName);
        if (osName.contains("Linux")){
            return true;
        }
        return false;
    }
}
